package cn.stylefeng.guns.topspeedCarRace;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;

import java.util.*;

public class ReadCarRace3 {
    private final static String url = "https://api.api68.com/pks/getPksHistoryList.do?date={}&lotCode=10037";

    public static void main(String[] args) {
//        JSONObject jsonObject = readRace();
//        jisuan(jsonObject);
        getCodes("2022-11-01", 1);
    }

    public static void getCodes(String beginDate, int num) {
//        String beginDate = "2022-10-01";
        DateTime date = DateUtil.parse(beginDate);
        String format = "yyyy-MM-dd";
        Map<String, Map<String, List<String>>> maps = Maps.newLinkedHashMap();
        List<String> dates = Lists.newArrayList();
        for (int i = 0; i < num; i++) {
            String dateStr = DateUtil.format(DateUtil.offsetDay(date, i), format);
            dates.add(dateStr);
            System.out.println(dateStr);
            JSONObject jsonObject = readRace(dateStr);
            Map<String, List<String>> map = jisuan(jsonObject);
            maps.put(dateStr, map);
        }
//        System.out.println(maps);
        print(maps, dates);
    }

    public static void print(Map<String, Map<String, List<String>>> maps, List<String> dates) {
//        StringBuffer sb = new StringBuffer();
//        for (int j = 0; j < dates.size(); j++) {
//            sb.append("{}\t");
//        }
//        for (int i = 0; i < 10; i++) {
////            StringBuffer sb = new StringBuffer();
//            //获取最大的行数
//            for (int j = 0; j < dates.size(); j++) {
//                int maxCol = 0;
//                String date = dates.get(j);
//                Map<String, List<String>> map = maps.get(date);
//                List<String> issues = map.get(String.valueOf(i));
//                if (issues.size() > maxCol) {
//                    maxCol = issues.size();
//                }
//            }
//            List<String> datas = Lists.newArrayList();
//            for (int j = 0; j < maxCol; j++) {
//                StringBuffer sb = new StringBuffer();
//                for (int k = 0; k < dates.size(); k++) {
//                    String date = dates.get(k);
//                    Map<String, List<String>> map = maps.get(date);
//                    List<String> issues = map.get(String.valueOf(i));
//                    if (issues.size() < j) {
//                        sb.append(issues.get(j));
//                    }
//                    sb.append("\t");
//                }
//                datas.add(sb.toString());
//            }
//
//
//
//        }
        for (int i = 0; i < 10; i++) {
//            StringBuffer sb = new StringBuffer();
            //获取最大的行数
            int maxCol = 0;
            for (int j = 0; j < dates.size(); j++) {
                String date = dates.get(j);
                Map<String, List<String>> map = maps.get(date);
                List<String> issues = map.get(String.valueOf(i));
                if (issues.size() > maxCol) {
                    maxCol = issues.size();
                }
            }
            List<String> datas = Lists.newArrayList();
            for (int j = 0; j < maxCol; j++) {
                StringBuffer sb = new StringBuffer();
                for (int k = 0; k < dates.size(); k++) {
                    String date = dates.get(k);
                    Map<String, List<String>> map = maps.get(date);
                    List<String> issues = map.get(String.valueOf(i));
                    if (issues.size() > j) {
                        sb.append(issues.get(j));
                    }
                    sb.append("\t").append("\t");
                }
                datas.add(sb.toString());
            }
//            System.out.println(datas);
            System.out.println("=============");
            for (int j = 0; j < datas.size(); j++) {
                System.out.println(datas.get(j));
            }

        }

    }

    public static JSONObject readRace(String date) {

        JSONObject params = new JSONObject();
        String data = HttpUtil.createGet(StrUtil.format(url, date))
                .execute().body();
        JSONObject jsonObject = JSONObject.parseObject(data);
//        JSONArray array = jsonObject.getJSONArray("data");
//        JSONObject jsonObject1 = array.getJSONObject(array.size() - 1);

//        String path = StrUtil.format("D:\\乱七八糟\\快双\\\\{}{}.txt", "极速赛车2", jsonObject1.getString("issue").substring(4));
//        System.out.println("path:" + path);
//        File file = new File(path);
//        FileUtil.writeString(data, file, Charset.defaultCharset());
        return jsonObject;
    }

    public static Map<String, List<String>> jisuan(JSONObject jsonObject) {
        JSONArray datas = jsonObject.getJSONObject("result").getJSONArray("data");
        List<CarRace> races = Lists.newArrayList();
        Integer issue = 1;
        for (int i = datas.size() - 1; i > 0; i--) {
            JSONObject data = datas.getJSONObject(i);
            String code = data.getString("preDrawCode");
            String[] codes = code.split(",");
            CarRace race = new CarRace(String.valueOf(issue++), parseInt(codes[0]), parseInt(codes[1]), parseInt(codes[2]),
                    parseInt(codes[3]), parseInt(codes[4]), parseInt(codes[5]),
                    parseInt(codes[6]), parseInt(codes[7]), parseInt(codes[8]),
                    parseInt(codes[9]), codes);
            races.add(race);
        }

        Map<String, List<String>> map = Maps.newHashMap();

        for (int i = 0; i < 10; i++) {
            List<String> print = getLXCode(races, i);
            map.put(String.valueOf(i), print);
//            System.out.println("<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>" + (i + 1));
        }
        return map;
    }

    //获取连续的code
    public static List<String> getLXCode(List<CarRace> races, Integer num) {
        Map<Integer, Integer> map = initMap();
        List<String> issues = Lists.newArrayList();
        for (int i = 0; i < races.size(); i++) {
            //最大的code
            Integer maxCode = sortByValue(map);

            CarRace race = races.get(i);
            Integer code = Integer.valueOf(race.getCodes()[num]);

            add(map, code);
            if (maxCode == code) {
//                System.out.println(StrUtil.format("{}\t{}", race.getIssue(), code));
//                System.out.println(StrUtil.format("{}", race.getIssue()));
                issues.add(race.getIssue());
            }
        }
        return issues;
    }

    public static void add(Map<Integer, Integer> map, Integer code) {
        for (int i = 1; i <= 10; i++) {
            if (i == code) {
                map.put(i, 0);
            } else {
                Integer num = map.get(i);
                map.put(i, ++num);
            }
        }
    }

    public static Integer sortByValue(Map<Integer, Integer> map) {
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        //升序排序
        Collections.sort(list, Comparator.comparing(Map.Entry::getValue));
        return list.get(list.size() - 1).getKey();
    }

    public static Map<Integer, Integer> initMap() {
        Map<Integer, Integer> map = Maps.newTreeMap();
        map.put(1, 0);
        map.put(2, 1);
        map.put(3, 2);
        map.put(4, 3);
        map.put(5, 4);
        map.put(6, 5);
        map.put(7, 6);
        map.put(8, 7);
        map.put(9, 8);
        map.put(10, 9);
        return map;
    }

    public static Integer parseInt(String code) {
        return Integer.valueOf(code);
    }

    @Data
    public static class CarRace {
        private String issue;
        private Integer code1;
        private Integer code2;
        private Integer code3;
        private Integer code4;
        private Integer code5;
        private Integer code6;
        private Integer code7;
        private Integer code8;
        private Integer code9;
        private Integer code10;
        private String[] codes;

        public CarRace(String issue, Integer code1, Integer code2, Integer code3,
                       Integer code4, Integer code5, Integer code6, Integer code7,
                       Integer code8, Integer code9, Integer code10) {
            this.issue = issue;
            this.code1 = code1;
            this.code2 = code2;
            this.code3 = code3;
            this.code4 = code4;
            this.code5 = code5;
            this.code6 = code6;
            this.code7 = code7;
            this.code8 = code8;
            this.code9 = code9;
            this.code10 = code10;
        }

        public CarRace(String issue, Integer code1, Integer code2, Integer code3, Integer code4,
                       Integer code5, Integer code6, Integer code7, Integer code8, Integer code9,
                       Integer code10, String[] codes) {
            this.issue = issue;
            this.code1 = code1;
            this.code2 = code2;
            this.code3 = code3;
            this.code4 = code4;
            this.code5 = code5;
            this.code6 = code6;
            this.code7 = code7;
            this.code8 = code8;
            this.code9 = code9;
            this.code10 = code10;
            this.codes = codes;
        }

        public Integer subIssue() {
            return Integer.valueOf(this.issue.substring(9));
        }
    }

    public static class LianXuCode {

    }
}
